package net.pimpas.web.tests.bb;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.faces.event.ActionEvent;
import javax.faces.model.SelectItem;

import net.pimpas.web.jsf.managedbean.ManagedBeanAnnotation;

@ManagedBeanAnnotation(beanName="mbCheckbox", beanScope="session")
public class CheckBoxMB {

	private List<SelectItem> values;
	private List<String> selecteds;
	private Boolean booleanSelected;
	
	public CheckBoxMB() {
		this.setValues(new ArrayList<SelectItem>());
		this.setSelecteds(new ArrayList<String>());
		this.setBooleanSelected(false);
		this.init( );
	}
	
	public void changeValue(ActionEvent evt) {
		this.setBooleanSelected(!this.getBooleanSelected());
		this.changeValues(evt);
	}
	
	private void init() {
		this.getValues().add(new SelectItem("Futball", "Futball"));
		this.getValues().add(new SelectItem("HandBall", "HandBall"));
		this.getValues().add(new SelectItem("Programming", "Programming"));
		this.getValues().add(new SelectItem("Basketball", "Basketball"));
	}
	
	public void changeValues(ActionEvent evt) {
		Random r = new Random(System.currentTimeMillis());
		int el1 = r.nextInt(4);
		int el2 = el1;
		while(el2 == el1) el2 = r.nextInt(4);
		
		for(int i=0;i<this.getSelecteds().size();i++)
			System.out.println("[Selected]: "+this.getSelecteds().get(i));
		
		this.getSelecteds().clear();
		
		this.getSelecteds().add((String) this.getValues().get(el1).getValue());
		this.getSelecteds().add((String) this.getValues().get(el2).getValue());
	}

	public List<String> getSelecteds() {
		return selecteds;
	}
	public void setSelecteds(List<String> selecteds) {
		this.selecteds = selecteds;
	}
	public List<SelectItem> getValues() {
		return values;
	}
	public void setValues(List<SelectItem> values) {
		this.values = values;
	}

	public Boolean getBooleanSelected() {
		return booleanSelected;
	}

	public void setBooleanSelected(Boolean booleanSelected) {
		this.booleanSelected = booleanSelected;
	}
}